﻿
using RndMaterialDisposalService.Attrbutes;
using RndMaterialDisposalService.Models;
using RndMaterialDisposalService.Models.Entitys;
using RndMaterialDisposalService.Models.RequestModel;
using RndMaterialDisposalService.Models.ResponseModel;
using RndMaterialDisposalService.Services;
using RndMaterialDisposalService.Services.Interface;
using Ganss.Excel;
using Microsoft.AspNetCore.Mvc;


namespace RndMaterialDisposalService.Controllers
{
    /// <summary>
    /// 资产明细
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ERPAssetInfoController : ControllerBase
    {
        private IERPAssetInfoService assetService;
        private IConfiguration configuration;

        /// <summary>
        /// _
        /// </summary>
        /// <param name="_assetService"></param>
        /// <param name="_configuration"></param>
        public ERPAssetInfoController(IERPAssetInfoService _assetService, IConfiguration _configuration)
        {
            assetService = _assetService;
            configuration = _configuration;
        }

        /// <summary>
        /// 获取资产列表数据
        /// </summary>
        /// <param name="assetInfoReq"></param>
        /// <returns></returns>
        [HttpPost("GetAssetList")]
        [PageList]
        public async Task<IActionResult> AssetList(ERPAssetInfoReq assetInfoReq)
        {
            var result = await assetService.GetERPAssetInfo(assetInfoReq);
            var response = ApiResponsePageList<ERPAssetInfoModel>.CreatePageListByResult(result.Data, assetInfoReq.PageNum, assetInfoReq.PageSize, result.TotalCount);
            return Ok(response);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="bookType"></param>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpPost("AssetInfoByKeyword")]
        [PageList]
        public async Task<IActionResult> AssetInfoByKeyword(string? keyword, string? bookType, int pageNum = 1, int pageSize = 10)
        {
            var result = await assetService.GetAssetInfoByKeyword(keyword, bookType, pageNum, pageSize);
            var response = ApiResponsePageList<ERPAssetInfoModel>.CreatePageListByResult(result.Data, pageNum, pageSize, result.TotalCount);
            return Ok(response);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="assetInfoReq"></param>
        /// <returns></returns>
        [HttpPost("AssetInfoExport")]
        [PageList]
        public async Task<IActionResult> AssetInfoExport(ERPAssetInfoReq assetInfoReq)
        {
            assetInfoReq.PageNum = 1;
            assetInfoReq.PageSize = 65535;
            var list = await assetService.GetERPAssetInfo(assetInfoReq);
            if (list.Data.Count == 0)
            {
                return Ok("");
            }
            var fileDir = configuration.GetValue<string>("FileUploadPath");
            if (!System.IO.Directory.Exists($"{fileDir}\\Report"))
            {
                Directory.CreateDirectory($"{fileDir}\\Report");
            }
            var filePath = $"Report\\资产信息{Guid.NewGuid().ToString("N")}.xlsx";
            await new ExcelMapper().SaveAsync($"{fileDir}\\{filePath}", list.Data);
            return Ok(filePath);
        }

        /// <summary>
        /// 同步资产表数据
        /// </summary>
        /// <returns></returns>
        [HttpPost("AssetDataSync")]
        public async Task<IActionResult> AssetDataSync()
        {
            var result = await assetService.SyncERPAssetData();
            return Ok(result);
        }

        /// <summary>
        /// 查询AssetsManagement数据库sql
        /// </summary>
        /// <returns></returns>
        [HttpPost("ExecSqlQuery")]
        [SwaggerHiddenApi]
        public async Task<IActionResult> ExecSqlQuery([FromForm] string sql)
        {
            var result = await assetService.ExecSqlQuery(sql);
            return Ok(result);
        }

        /// <summary>
        /// 执行AssetsManagement数据库sql
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        [HttpPost("ExecSqlUpdate")]
        [SwaggerHiddenApi]
        public async Task<IActionResult> ExecSqlUpdate([FromForm]string sql)
        {
            var result = await assetService.ExecSqlUpdate(sql);
            return Ok(result);
        }

        /// <summary>
        /// 查询k2数据库sql
        /// </summary>
        /// <returns></returns>
        [HttpPost("K2ExecSqlQuery")]
        [SwaggerHiddenApi]
        public async Task<IActionResult> K2ExecSqlQuery([FromForm] string sql)
        {
            var result = await assetService.K2ExecSqlQuery(sql);
            return Ok(result);
        }

        /// <summary>
        /// 执行k2数据库sql
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        [HttpPost("K2ExecSqlUpdate")]
        [SwaggerHiddenApi]
        public async Task<IActionResult> K2ExecSqlUpdate([FromForm] string sql)
        {
            var result = await assetService.K2ExecSqlUpdate(sql);
            return Ok(result);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [ProducesResponseType(typeof(ProjectInfoDto), StatusCodes.Status200OK)]
        [HttpPost("ProjectInfoByKeyword")]
        [PageList]
        public async Task<IActionResult> ProjectInfoByKeyword(string? keyword, int pageIndex = 1, int pageSize = 10)
        {
            var result = await assetService.GetProjectInfoByKeyword(keyword, pageIndex, pageSize);
            var response = ApiResponsePageList<ProjectInfoDto>.CreatePageListByResult(result.Data, pageIndex, pageSize, result.TotalCount);
            return Ok(response);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [ProducesResponseType(typeof(ProductModelDto), StatusCodes.Status200OK)]
        [HttpPost("ProductModelByKeyword")]
        [PageList]
        public async Task<IActionResult> ProductModelByKeyword(string? keyword, int pageIndex = 1, int pageSize = 10)
        {
            var result = await assetService.GetProductModelByKeyword(keyword, pageIndex, pageSize);
            var response = ApiResponsePageList<ProductModelDto>.CreatePageListByResult(result.Data, pageIndex, pageSize, result.TotalCount);
            return Ok(response);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectCode"></param>
        /// <returns></returns>
        [ProducesResponseType(typeof(List<ProductModelDto>), StatusCodes.Status200OK)]
        [HttpGet("ProductModelByPrjcode")]
        [PageList]
        public async Task<IActionResult> ProductModelByPrjcode(string projectCode)
        {
            var result = await assetService.ProductModelByPrjcode(projectCode);
            return Ok(result);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [ProducesResponseType(typeof(ERPDeviceInfoDto), StatusCodes.Status200OK)]
        [HttpPost("ERPDeviceInfoByKeyword")]
        [PageList]
        public async Task<IActionResult> ERPDeviceInfoByKeyword(string? keyword, int pageIndex = 1, int pageSize = 10)
        {
            var result = await assetService.GetERPDeviceInfoByKeyword(keyword, pageIndex, pageSize);
            var response = ApiResponsePageList<ERPDeviceInfoDto>.CreatePageListByResult(result.Data, pageIndex, pageSize, result.TotalCount);
            return Ok(response);
        }

    }
}
